﻿@{
    ViewBag.Title = "TestFeed";    
    Layout = "~/Views/Shared/_Layout.Test.cshtml";
}

@CoffeeScripts.Render("~/CoffeeScripts/shared/coffee");
@CoffeeScripts.Render("~/CoffeeScripts/models/coffee");
@CoffeeScripts.Render("~/CoffeeScripts/controllers/coffee");
<script type="text/coffeescript">
_feed14path = "/CoffeeScripts/tests/data/atom/14.txt";

asyncTest( "Parse FeedEvent from Atom feed", () ->  
  $.ajax({
    url : _feed14path
    dataType : 'xml'
    })
  .done((xhr, s, e) =>
        ok(xhr != null, "_feed14path is null")             
        xml = xhr
        firstItem = $(xml).find('item').first()
    
        #console.log firstItem
        e = Cofe.Event.fromXml(firstItem)  
        ok(e.author isnt "" and e.author isnt undefined, "author")
        ok(e.id isnt "" and e.id isnt undefined, "id")
        ok(e.categories isnt undefined, "categories")
        ok(e.categories.entry isnt undefined, "categories\entry")
        ok(e.links isnt undefined, "links")
        ok(e.links.self isnt undefined, "links\self")
           
        ei = Cofe.EventItem.fromEvent(e)
        console.log ei

        start() 
      )
  .error((xhr, s, e) => 
        ok(false, "_feed14path - " + e)
        start()
      )  
)  

asyncTest( "Parse Atom feed, using Feed.fromUrl", () ->  
    feed = Cofe.Feed.fromUrl(_feed14path)
    feed.bind("refreshed", () ->
       start()
       ok(feed.author isnt "" and feed.author isnt undefined, "author")
       ok(feed.links.self isnt undefined, "links\self")
       ok(feed.events.length is 4, "events count") 
    )
    feed.fetch()
    
)


asyncTest( "Parse Atom feed", () ->  
  $.ajax({
    url : _feed14path
    dataType : 'xml'
    })
  .done((xhr, s, e) =>
        ok(xhr != null, "_feed14path is null")             
        xml = xhr     
        feed = Cofe.Feed.fromXml(xhr)
        console.log feed
        ok(feed.id isnt "" and feed.id isnt undefined, "id")
        ok(feed.links isnt undefined, "links")
        ok(feed.links.self isnt undefined, "links\self")
        ok(feed.events isnt undefined, "events")
        ok(feed.events.length is 4, "events count")                

        fi = Cofe.FeedItem.fromFeed(feed)
        console.log fi
        ok(fi.eventItems.length is 4, "eventItems count")     

        start()
      )
  .error((xhr, s, e) => 
        ok(false, "_feed14path - " + e)
        start()
      )  
)  


asyncTest( "Parse Atom feed, using FeedItem.fromUrl", () ->  
    feedItem = Cofe.FeedItem.fromUrl(_feed14path)
    ok(feedItem.feed.status is "notready", "notready")
    ok(feedItem.feed isnt undefined, "feed")
    feedItem.bind("refreshed", (fi) ->       
       ok(fi.feed.status is "ready", "ready != " + fi.feed.status )
       ok(fi.feed.author isnt "" and fi.feed.author isnt undefined, "author")
       ok(fi.feed.links.self isnt undefined, "links\self")
       ok(fi.eventItems.length is 4, "events count") 
       ok(!fi.hasNext, "!hasNext")
       ok(fi.hasPrev, "hasPrev")
       start()
    )
    feedItem.load()
    
)


</script>